package com.example.ttt.functions;

import com.example.ttt.jcomp.JFrameComponent;
import com.example.ttt.utils.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/*
* 左边侧边栏的选中事件
* */

@Component
@Slf4j
public class Button_LeftSelectedEvent {

    @Autowired
    private JFrameComponent jFrameComponent;
    @Autowired
    private TableFill tableFill;
    @Autowired
    private GetDocumentList documentList;
    @Autowired
    private Environment environment;
    @Autowired
    private OSSClient ossClient;

    /*
    * 判断选择的栏目进行修改样式，并重新填充播放列表
    * */
    public void selectedEvent(JLabel jLabel) {
        JLabel myLiking = jFrameComponent.getMyLiking();
        JLabel localMusic = jFrameComponent.getLocalMusic();
        JLabel myPlaylist = jFrameComponent.getMyPlaylist();
        JLabel moreMusic = jFrameComponent.getMoreMusic();

        if (jLabel == localMusic) {
            serBackground(jLabel);
            myLiking.setBackground(null);
            myPlaylist.setBackground(null);
            moreMusic.setBackground(null);
            // 替换表格数据
            String scannerFilePath = environment.getProperty("scannerFilePath");
            List localMusicList = documentList.Get_List(scannerFilePath);
            tableFill.tableFillData(localMusicList);
            log.info("{} 数据更新成功",localMusic.getText());
        } else if (jLabel == myLiking) {
            serBackground(jLabel);
            localMusic.setBackground(null);
            myPlaylist.setBackground(null);
            moreMusic.setBackground(null);
            // 我的喜欢  替换表格数据
            List myLikingLists = new ArrayList();
            tableFill.tableFillData(myLikingLists);
            log.info("{} 数据更新成功",myLiking.getText());
        } else if (jLabel == myPlaylist) {
            serBackground(jLabel);
            localMusic.setBackground(null);
            myLiking.setBackground(null);
            moreMusic.setBackground(null);
            // 我的歌单 替换表格数据
            List myPlaylistList = new ArrayList();
            tableFill.tableFillData(myPlaylistList);
            log.info("{} 数据更新成功",myPlaylist.getText());
        } else if (jLabel == moreMusic) {
            serBackground(jLabel);
            localMusic.setBackground(null);
            myLiking.setBackground(null);
            myPlaylist.setBackground(null);
            ossClient.OSSInit();
            new Thread(){
                @Override
                public void run() {
                    // 更多音乐 替换表格数据
                    ArrayList<String> moreMusicList = ossClient.getObjectList("adjoinme");
                    tableFill.tableFillData(moreMusicList);
                    log.info("{} 数据更新成功",moreMusic.getText());
                }
            }.start();
        }
    }

    private void serBackground(JLabel jLabel){
        jLabel.setBackground(Color.orange);
    }
}
